<?php
//include($_SERVER['DOCUMENT_ROOT'] . "/admin/includes/head.php");
include($_SERVER['DOCUMENT_ROOT'] . "/admin/includes/Interceptor.php");
include($_SERVER['DOCUMENT_ROOT'] . '/static/db/connect.php');
require_once $_SERVER['DOCUMENT_ROOT'] . '/autoload.php';

use Qiniu\Auth;
use Qiniu\Storage\BucketManager;

$accessKey = 'Vr2infZAJEsN8MVWW-KRaFDtyvKbW7YEPIOcudlL';
$secretKey = '-k0wf3uG7kyQIeyRm0OD0VSC3WUK-upZtk7U59tu';
$bucket = 'softwarebox';
$auth = new Auth($accessKey, $secretKey);
$bucketManager = new BucketManager($auth);
// 要列取文件的公共前缀
$prefix = 'software/';
// 上次列举返回的位置标记，作为本次列举的起点信息。
$marker = '';
// 本次列举的条目数
$limit = 200;
$delimiter = '';
$softwareList = array();

// 列举文件
do {
    list($ret, $err) = $bucketManager->listFiles($bucket, $prefix, $marker, $limit, $delimiter);
    if ($err !== null) {
        var_dump($err);
    } else {
        $marker = null;
        if (array_key_exists('marker', $ret)) {
            $marker = $ret['marker'];
        }
        if (count($ret['items']) > 0) {
            $softwareList = $ret['items'];
        }
    }
} while (!empty($marker));

if ($_SERVER['REQUEST_METHOD'] == "GET") { ?>

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>layui</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link href="/static/css/resume/bootstrap.min.css" rel="stylesheet" type="text/css">
        <link rel="stylesheet" href="/admin/static/layui/css/layui.css" media="all">
    </head>
    <body>

    <form class="layui-form layui-form-pane" action="" method="post">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">软件名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required" placeholder="请输入软件名称" class="layui-input">
                </div>
            </div>
            <div class="layui-inline" style="width: 200px">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <select name="sort">
                        <option value="0">0</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">图标地址</label>
            <div class="layui-input-block">
                <input type="text" name="img" placeholder="请输入图标地址" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">软件简介</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入软件简介" class="layui-textarea" name="outline"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">MAC应用</label>
            <div class="layui-input-inline">
                <select name="mac" lay-search>
                    <option value="">请选择MAC或全系列</option>
                    <?php foreach ($softwareList as $row):
                        if (strpos($row['key'], '/mac/')) { ?>
                            <option value="<?php echo $row['key']; ?>"><?php echo str_replace('software/', '', $row['key']); ?></option>
                        <?php }
                    endforeach; ?>
                </select>
            </div>
            <div class="layui-input-inline">
                <input type="text" name="mac_version" placeholder="版本号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">WIN应用</label>
            <div class="layui-input-inline">
                <select name="win" lay-search>
                    <option value="">请选择WIN系列</option>
                    <?php foreach ($softwareList as $row):
                        if (strpos($row['key'], '/win/')) { ?>
                            <option value="<?php echo $row['key']; ?>"><?php echo str_replace('software/', '', $row['key']); ?></option>
                        <?php }
                    endforeach; ?>
                </select>
            </div>
            <div class="layui-input-inline">
                <input type="text" name="win_version" placeholder="版本号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">系列应用</label>
            <div class="layui-input-inline">
                <select name="all_ver" lay-search>
                    <option value="">请选择ALL系列</option>
                    <?php foreach ($softwareList as $row):
                        if (strpos($row['key'], '/all/')) { ?>
                            <option value="<?php echo $row['key']; ?>"><?php echo str_replace('software/', '', $row['key']); ?></option>
                        <?php }
                    endforeach; ?>
                </select>
            </div>
            <div class="layui-input-inline">
                <input type="text" name="version" placeholder="版本号" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" style="text-align:right">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="addData">立即提交</button>
        </div>
    </form>

    <script src="/admin/static/layui/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form', 'layedit'], function () {
            var form = layui.form, $ = layui.jquery;

            //监听提交
            form.on('submit(addData)', function (data) {
                $.post("/admin/view/software/addData.php", data.field, function (res) {
                    if (res == 1) {
                        layui.layer.msg("新增成功！");
                        setTimeout(function () {
                            parent.closeModal();
                        }, 1000);
                    }
                });
                return false;
            });
        });
    </script>
    </body>
    </html>

    <?php
} else {
    @$name = $_POST["name"];
    if (isset($name) && $name != "") {
        $url = 'https://7xo65x.com1.z0.glb.clouddn.com/';
        $img = $_POST["img"];
        $mac = $_POST["mac"] ? $url . $_POST["mac"] : '';
        $outline = $_POST["outline"];
        $sort = $_POST["sort"];
        $version = $_POST["mac_version"] ?: $_POST["version"];
        $win = $_POST["win"] ? $url . $_POST["win"] : '';;
        $win_version = $_POST["win_version"];
        $all_ver = '';
        $sql = "insert into qt_software_box (name,img,outline,win,mac,all_ver,version,win_version,sort) VALUES ('$name', '$img', '$outline', '$win', '$mac','$all_ver','$version','$win_version','$sort')";
        $mysqli->query($sql);
        echo 1;
    } else {
        echo 0;
    }
}
$mysqli->close();

?>
